home *** CD-ROM | disk | FTP | other *** search
- Path: news.itd.umich.edu!not-for-mail
- From: jlthomas@umd.umich.edu (Jeffrey L. Thomas)
- Newsgroups: comp.lang.c
- Subject: Re: How to get program using Fork() and Pipe()
- Date: 29 Feb 1996 16:27:17 -0500
- Organization: Univerisity of Michigan - Dearborn
- Message-ID: <4h55nl$qga@null.umd.umich.edu>
- References: <4gic02$6l0@news.mty.itesm.mx>
- NNTP-Posting-Host: null.umd.umich.edu
- NNTP-Posting-User: jlthomas
- X-Newsreader: NN version 6.5.0 #3 (NOV)
-
- al535994@academ10.mty.itesm.mx (ARTURO RAFAEL LOPEZ CASTRO) writes:
-
- >I would like to know if someone could give program examples using fork and pipe because I have trouble using them.
-
- >Thanks.
-
-
- int fork_off(prog)
- char *prog;
- {
- int ipd[2], opd[2], pid;
-
- /* making the input and output pipes */
- if (pipe(ipd)<0 || pipe(opd)) { perror("pipe"); return(1); }
-
- if ((pid= fork())<0) { perror("pipe"); return(1); }
-
- if (pid==0) { /* exec the program */
- dup2(ipd[1],1); /* resetting stdin and stdout for child */
- dup2(opd[0],0);
- execlp("prog", "prog", NULL);
- /* this line only get executed on error */
- perror("exec"); exit(1);
- }
-
- dup2(ipd[0],0); /* resetting stdin and stdout for the parent */
- dup2(opd[1],1);
-
- return(0);
- }
-
-
- and in your main program
-
- ...
- char prog[]= "some_program";
- ...
- if (fork_off(prog)!=0) exit(1);
-
- /*
- * now stdin if comming in from the program and stdout is going to the program
- */
-
- --
- \|||/ _Spike_Man_ ____ | Jeffery L. Thomas | "The important thing is
- ^.^ The Internet's / \ | jlthomas@umich.edu | not to stop questioning.
- v first superhero \ SM / | "This time it will | Curiosity has it own
- "more than a cute .sig" \__/ | surely run" - anon | reason for existing."
-